package com.haople.sso.web.controller;

import java.io.IOException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.haople.sso.web.captcha.Captcha;
import com.haople.sso.web.captcha.GifCaptcha;
import com.haople.sso.web.captcha.PngCaptcha;

/**
 * 验证码
 * @author Administrator
 *
 */
@Controller
public class CaptchaController {
	
	private static final Logger logger=Logger.getLogger(CaptchaController.class);
	private static final String SSO_CAPTCHA="sso_captcha";

	/**
	 * 获取验证码
	 */
	@RequestMapping(value="/captcha")
	public void captcha(HttpServletResponse response,HttpSession httpSession){
		ServletOutputStream outputStream=null;
		try {
			response.setContentType("image/png");
			outputStream=response.getOutputStream();
			Captcha captcha = new PngCaptcha(130,37,6);//png格式动画验证码
			captcha.out(outputStream);
			logger.info("生成验证码："+captcha.value());
			httpSession.setAttribute(SSO_CAPTCHA, captcha.value());
		} catch (Exception e) {
			logger.error("验证码异常", e);
		}finally{
			try {
				if(outputStream!=null){
					outputStream.flush();
					outputStream.close();
				}
			} catch (IOException e) {
				logger.error("验证码异常", e);
			}
		}
	}
}
